package com.huopaolan.yohuoshop.Mvp.Presenter;

import android.util.Log;

import com.huopaolan.lib_core.Mvp.Presenter.BasePresenter;
import com.huopaolan.yohuoshop.Bean.BrandEntity;
import com.huopaolan.yohuoshop.Bean.BrandListEntity;
import com.huopaolan.yohuoshop.Mvp.Contract.BrandFragmentContract;
import com.huopaolan.yohuoshop.Mvp.View.Fragment.BrandFragment;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class BrandFragmentPresenter extends BasePresenter<BrandFragmentContract.BrandIView, BrandFragmentContract.BrandIModel> {
    @Inject
    public BrandFragmentPresenter(BrandFragmentContract.BrandIView brandIView, BrandFragmentContract.BrandIModel brandIModel) {
        super(brandIView, brandIModel);
    }

    //品牌请求
    public void brandRequest() {
        Log.d("brandRequest", "数据请求成功");
        JSONObject brand = new JSONObject();
        try {
            brand.put("menuid", "1");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String request = brand.toString();
        m.brand_list_request(request).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BrandEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BrandEntity entity) {
                        Log.d("onNext", "请求成功");
                        buildLetters(entity);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 生成通讯录
     */
    private void buildLetters(BrandEntity entity) {
        //首字母集合
        List<String> letteres = buildLetteres(entity);
        //二级菜单集合
        List<BrandListEntity> brandListEntities = buildBrandList(letteres, entity);
        //新入驻品牌集合
        List<BrandEntity.Values> newBrandEntitys = buildRecommend(entity);
        //热门品牌集合
        List<BrandEntity.Values> hotBrandEntitys = buildHot(entity);
        //推荐品牌
        List<BrandEntity.Values> recommendEntitys = buildRecommend(entity);
        //广告集合
        List<BrandEntity.Values> bannerEntities = buildRecommend(entity);
        //向brand回调
        Map<String, List> map = new HashMap<>();
        map.put("letteres", letteres);
        map.put("brandListEntities", brandListEntities);
        map.put("newBrandEntitys", newBrandEntitys);
        map.put("hotBrandEntitys", hotBrandEntitys);
        map.put("recommendEntitys", recommendEntitys);
        map.put("bannerEntities", bannerEntities);
        v.brandSuccess(map);
    }

    private List<BrandEntity.Values> buildHot(BrandEntity entity) {
        List<BrandEntity.Values> hotList = new ArrayList<>();
        for (BrandEntity.Values values : entity.values) {
            if (values.hot_tag.equals("true")) {
                hotList.add(values);
            }
        }
        return hotList;
    }

    //构建热门品牌列表
    private List<BrandEntity.Values> buildRecommend(BrandEntity entity) {
        List<BrandEntity.Values> list = new ArrayList<>();
        for (int i = 0; i < entity.values.size(); i++) {
            list.add(entity.values.get(i));
            if (i == 8) {
                break;
            }
        }
        return list;
    }

    //构建二级列表
    private List<BrandListEntity> buildBrandList(List<String> letters, BrandEntity entity) {
        List<BrandListEntity> listEntities = new ArrayList<>();
        for (String letter : letters) {
            BrandListEntity brandListEntity = new BrandListEntity();
            brandListEntity.brand_letter = letter;
            brandListEntity.type = 1;
            listEntities.add(brandListEntity);
            for (BrandEntity.Values values : entity.values) {
                if (brandListEntity.brand_letter.equals(values.brand_letter)) {
                    BrandListEntity child = new BrandListEntity();
                    child.type = 2;
                    child.brand_name = values.brand_name;
                    child.brand_time = values.brand_time;
                    child.brand_bg = values.brand_bg;
                    child.brand_icon = values.brand_icon;
                    child.brand_id = values.brand_id;
                    child.hot_tag = values.hot_tag;
                    listEntities.add(child);
                }
            }
        }
        return listEntities;
    }

    private List<String> buildLetteres(BrandEntity entity) {
        List<String> letters = new ArrayList<>();
        for (BrandEntity.Values values : entity.values) {
            letters.add(values.brand_letter);
        }
        //首字母去重
        Set<String> set = new TreeSet<>(letters);
        letters.clear();
        letters.addAll(set);
        Collections.sort(letters);
        return letters;
    }
}
